package demo.screens
{
	import com.cheezeworld.screens.IScreenItem;
	import com.cheezeworld.screens.Root;
	import com.cheezeworld.utils.Input;
	import com.urbansquall.ginger.Animation;
	import com.urbansquall.ginger.AnimationPlayer;
	import com.urbansquall.ginger.events.AnimationEvent;
	import com.urbansquall.ginger.tools.AnimationBuilder;
	import com.urbansquall.ui.CustomTextField;
	
	import demo.BitmapDataLibrary;
	
	import flash.display.BitmapData;
	import flash.geom.Rectangle;

	public class EventsDemo extends DemoScreen
	{
		public function EventsDemo(a_parentScreen:IScreenItem=null)
		{
			super(a_parentScreen);
			
			screenLabel = "Multiple Animations \n + \n Animation Events";
			
			var bdLib:BitmapDataLibrary = new BitmapDataLibrary();
			var bd:BitmapData = bdLib.lookupBitmapData( BitmapDataLibrary.BOMB_TS );
			
			// Create bomb idle animation
			var animation:Animation = AnimationBuilder.importStrip( 12, bd, 16, 16, 4, 0, 0, 120, -8, -8 );
			animation.isLooping = true;
			
			m_bomb = new AnimationPlayer();
			m_bomb.addAnimation( "idle", animation );
			
			// Create exploding animation
			animation = AnimationBuilder.importStrip( 12, bd, 32, 32, 4, 0, 16, 120, -16, -16 );		
			m_bomb.addAnimation( "explode", animation );
			
			m_bomb.x = Root.WIDTH / 2;
			m_bomb.y = Root.HEIGHT / 2;			
			addChild( m_bomb );
			
			m_bomb.addEventListener( AnimationEvent.CHANGE, onAnimationEvent );
			
			var txt:CustomTextField = new CustomTextField( "Click the Mouse to blow up the bomb!", 0x0, 18 );
			txt.x = m_bomb.x - txt.width / 2;
			txt.y = m_bomb.y - txt.height - 20;
			addChild( txt );
			
			m_eventTxt = "";
			m_tf = new CustomTextField( m_eventTxt, 0xFF0000, 12 );
			m_tf.x = Root.WIDTH / 2;
			m_tf.y = Root.HEIGHT - 100;
			addChild( m_tf );
		}
		
		public override function update(a_timePassed:int):void
		{
			if( m_bomb != null )
			{
				m_bomb.update( a_timePassed );
				
				if( Input.instance.isMousePressed )
				{
					m_bomb.play( "explode" );
				}				
			}
				
		}
		
		// -- PRIVATE --
		
		private function onAnimationEvent( e:AnimationEvent ) : void
		{
			trace( "Animation Event! Begin: " + e.begin + " ID: " + e.animationID );
			m_eventTxt += "\n Animation Event: ID: " + e.animationID + " " + ( e.begin ? "begin" : "end" );
			m_tf.text = m_eventTxt;
			
			if( e.begin == false && e.animationID == "explode" )
			{
				removeChild( m_bomb );
				m_bomb.removeEventListener( AnimationEvent.CHANGE, onAnimationEvent );
				m_bomb = null;
			}
			
			//m_bomb.play( "explode" );
		}
		
		private var m_tf:CustomTextField;
		private var m_eventTxt:String;
		private var m_bomb:AnimationPlayer;
		
	}
}